package com.tengju.user.infrastructure.repository.csm.dealer;

import com.tengju.user.domain.model.csm.dealer.CrmDealerInfo;
import com.tengju.user.domain.model.csm.dealer.CrmDealerInfoRepository;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.CrmDealerDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.data.CrmDealerDOCustomMapper;
import com.tengju.user.infrastructure.repository.convert.CrmDealerInfoConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: kezunfa
 * @Date: 2021/4/22 15:15
 * @Description:
 */
@Repository
@Slf4j
public class CrmDealerInfoRepositoryImpl implements CrmDealerInfoRepository {

    @Resource
    private CrmDealerDOCustomMapper crmDealerDOCustomMapper;

    @Autowired
    private UserInfoRepository userInfoRepository;

    @Override
    public List<CrmDealerInfo> getCrmDealerInfoList(Long idCode, Integer level,
                                                    String startTime, String endTime,
                                                    Long areaSupIdCode, List<Long> genSecIdCodeList,
                                                    Long inviteCode,
                                                    String goodNumber,
                                                    String weChat,
                                                    String mobile,
                                                    int offset, int limit) {

        // 通过手机号获取idCode
        UserInfo userInfo = userInfoRepository.selectByMobile(mobile);
        Long mobileIdCode = userInfo == null ? null : userInfo.getIdCode() == null ? null : userInfo.getIdCode().getValue();

        List<CrmDealerDO> crmDealerDOList = crmDealerDOCustomMapper.selectCrmDealer(idCode, level,
                startTime, endTime, areaSupIdCode, genSecIdCodeList, inviteCode, goodNumber, weChat,
                mobile, mobileIdCode, offset, limit);

        return crmDealerDOList.stream()
                .map(CrmDealerInfoConvert::toModel)
                .collect(Collectors.toList());
    }

    @Override
    public int getCrmDealerInfoCount(Long idCode, Integer level,
                                     String startTime, String endTime,
                                     Long areaSupIdCode, List<Long> genSecIdCodeList,
                                     Long inviteCode,
                                     String goodNumber,
                                     String weChat,
                                     String mobile) {

        // 通过手机号获取idCode
        UserInfo userInfo = userInfoRepository.selectByMobile(mobile);
        Long mobileIdCode = userInfo == null ? null : userInfo.getIdCode() == null ? null : userInfo.getIdCode().getValue();

        return crmDealerDOCustomMapper.getCrmDealerCount(idCode, level,
                startTime, endTime,
                areaSupIdCode, genSecIdCodeList,
                inviteCode,
                goodNumber,
                weChat,
                mobile,
                mobileIdCode);

    }

    @Override
    public CrmDealerInfo getCrmDealerSupInfo(UserIdCode userIdCode) {
        if (userIdCode == null) {
            return null;
        }
        CrmDealerDO crmDealerDO = crmDealerDOCustomMapper.getCrmDealerSupInfo(userIdCode.getValue());
        return CrmDealerInfoConvert.toModel(crmDealerDO);
    }

    @Override
    public List<CrmDealerInfo> getAll() {
        List<CrmDealerDO> crmDealerDOList = crmDealerDOCustomMapper.getAll();
        return crmDealerDOList.stream()
                .map(CrmDealerInfoConvert::toModel)
                .collect(Collectors.toList());
    }

    @Override
    public List<CrmDealerInfo> getCrmDealerNameInfo() {
        List<CrmDealerDO> crmDealerDOList = crmDealerDOCustomMapper.getCrmDealerNameInfo();
        return crmDealerDOList.stream()
                .map(CrmDealerInfoConvert::toModel)
                .collect(Collectors.toList());
    }

    @Override
    public List<CrmDealerInfo> listCrmDealerInfo(List<UserIdCode> idCodeList) {
        if (CollectionUtils.isEmpty(idCodeList)) {
            return Collections.emptyList();
        }
        List<Long> idCodeVals = idCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        List<CrmDealerDO> crmDealerDOList = crmDealerDOCustomMapper.listCrmDealerInfo(idCodeVals);
        return crmDealerDOList.stream()
                .map(CrmDealerInfoConvert::toModel)
                .collect(Collectors.toList());
    }
}
